package com.hz.score.ind.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hz.score.common.dto.PageDto;
import com.hz.score.common.util.ConvertUtil;
import com.hz.score.ind.dto.IndInfoDto;
import com.hz.score.ind.mapper.IndMapper;
import com.hz.score.ind.model.IndInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class IndService {

    @Resource
    IndMapper indMapper;

    public PageDto<IndInfoDto> pageIndEnt(IndInfoDto indInfoDto, Integer pageSize, Integer pageNum) {
        QueryWrapper wrapper = new QueryWrapper();
        if (indInfoDto.getName() != null) wrapper.eq("name", indInfoDto.getName());
        if (indInfoDto.getBizTyp() != null) wrapper.eq("biz_typ", indInfoDto.getBizTyp());
        return ConvertUtil.convertPage(indMapper.selectPage(new Page<>(pageNum, pageSize), wrapper), IndInfoDto.class);
    }

    public int newIndEnt(IndInfoDto indInfoDto) {
        IndInfo indInfo = new IndInfo();
        BeanUtils.copyProperties(indInfoDto, indInfo);
        return indMapper.insert(indInfo);
    }

    public int updateIndEnt(IndInfoDto indInfoDto) {
        IndInfo indInfo = new IndInfo();
        BeanUtils.copyProperties(indInfoDto, indInfo);
        return indMapper.updateById(indInfo);
    }

    public int delIndEnt(IndInfoDto indInfoDto) {
        IndInfo indInfo = new IndInfo();
        BeanUtils.copyProperties(indInfoDto, indInfo);
        return indMapper.deleteById(indInfo);
    }

    public IndInfoDto getIndEnt(Long id) {
        IndInfo indInfo =  indMapper.selectById(id);
        IndInfoDto IndInfoDto = new IndInfoDto();
        BeanUtils.copyProperties(indInfo, IndInfoDto);
        return IndInfoDto;
    }

    public List<IndInfoDto> getIndEnts(List<Long> ids) {
        List<IndInfoDto> indInfoDtoList = new ArrayList<>();
        List<IndInfo> indInfoList =  indMapper.selectBatchIds(ids);
        indInfoList.forEach(indInfo -> {
            IndInfoDto IndInfoDto = new IndInfoDto();
            BeanUtils.copyProperties(indInfo, IndInfoDto);
            indInfoDtoList.add(IndInfoDto);
        });

        return indInfoDtoList;
    }
}
